package com.szrt.day20170421.service.impl;

import java.util.List;

import com.szrt.day20170421.entity.Student;
import com.szrt.day20170421.service.StudentService;

public class StudentServiceImpl implements StudentService {

	@Override
	public void init(List<Student> studentList) {
		
		Student student1 = new Student("170001","123456","贾梦菊","女",22,"安徽省","宿州市","15889627151",110,105,125);
		Student student2 = new Student("170002","123456","罗智壕","男",25,"安徽省","合肥市","15866677151",100,90,135);
		Student student3 = new Student("170003","123456","贾静雯","女",27,"安徽省","芜湖市","15677779999",120,115,130);
		Student student4 = new Student("170004","123456","靳志辉","男",25,"广东省","深圳市","12344445555",90,115,100);
		Student student5 = new Student("170005","123456","段誉","男",23,"广东省","广州市","13566667777",130,95,115);
		Student student6 = new Student("170006","123456","张杰","男",24,"四川省","成都市","18866668888",120,135,106);
		Student student7 = new Student("170007","123456","谢娜","女",21,"四川省","成都市","18672637638",120,85,105);
		
		studentList.add(student1);
		studentList.add(student2);
		studentList.add(student3);
		studentList.add(student4);
		studentList.add(student5);
		studentList.add(student6);
		studentList.add(student7);
	}

	@Override
	public void printStudents(List<Student> studentList) {
		for(Student student: studentList){
			System.out.println(student);
		}
		
	}

	@Override
	public void deleteById(List<Student> studentList, String id) {
		
		for(int index =0; index<studentList.size();index++){
			Student student = studentList.get(index);
			
			if(id.equals(student.getIdExam())){
				//找到
				studentList.remove(index);
			}
		}
		
		/*for(Student student : studentList){
			if(id.equals(student.getIdExam())){
				studentList.remove(student);
			}
		}*/
		
	}

	@Override
	public int updateChineseById(List<Student> studentList, String id, int newScoce) {
		
		int counter =0;
		for(Student student : studentList){
			if(id.equals(student.getIdExam())){
				
				counter++;
				//修改对应的语文的成绩
				student.setChineseScore(newScoce);
			}
		}
		
		return counter;
		
	}

	@Override
	public boolean checkLogin(List<Student> studentList, String phone, String password) {
		
		boolean flag= false;
		
		for(Student student:studentList){
			
			if(phone.equals(student.getPhone()) && password.equals(student.getPassword())){
				//成功
				flag = true;
			}
			
		}
		
		return flag;
	}

	@Override
	public Student findMessageByPhone(List<Student> studentList, String phone) {
		for(Student student:studentList){
			
			if(phone.equals(student.getPhone())){
				//成功
				return student;
			}
			
		}
		
		return null;
	}

	@Override
	public Student findMaxOfCurrentCity(List<Student> studentList, String city) {
		
		
		int socre=0;
		
		Student resultStudent = new Student();
		
		for(Student student:studentList){
			
			
			String cy = student.getHuji();
			
			if(cy.equals(city)){
				
				int totalScore = student.getChineseScore()+ student.getEnglishScore()+student.getMathScore();
				
				if(totalScore > socre){
					socre =  totalScore;
					resultStudent = student;
				}
			}
			
		}
		
		return resultStudent;
	}

}
